// echo partial file, should not compile along

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using Utils;
namespace GongfaEffect;

using GameData.Domains;

public partial class GongfaEffect {
//! 我终于开始对战斗功法下毒手了
    public static class XiaoZongYueGong {
        public static short Defkey_XiaoZongYueGong=725; // 目前这个方法并没有被暴露在defkey之外，因此只能如此使用。
        public static void Patch(RobustHarmonyInstance HarmonyInstance, string ModIdStr){
            HarmonyInstance.PatchAll(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
        }
        [HarmonyPostfix]
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.Agile.XiaoZongYueGong),"OnEnable")]
        public static unsafe void OnEnable(GameData.Domains.SpecialEffect.CombatSkill.NoSect.Agile.XiaoZongYueGong __instance, bool ___IsDirect, GameData.Common.DataContext context) {
            GameData.Domains.Character.Character character;
            if(DomainManager.Character.TryGetElement_Objects(__instance.CharacterId, out character)){
                var loc=character.GetLocation();
                if(!loc.IsValid())return;
                var areaId=loc.AreaId;
                var blockId=loc.BlockId;
                var mbdata=new List<GameData.Domains.Map.MapBlockData>();
                DomainManager.Map.GetNeighborBlocks(areaId, blockId, mbdata, 3);
                foreach(var mbd in mbdata){
                    if(mbd.CanChangeBlockType()){
                        if(___IsDirect){
                            if(mbd.TemplateId>=109 && mbd.TemplateId<=124){
    //                            var target=(short)context.Random.Next(39,109);
    //                            if(target>=57 && target<=92){
    //                                if(target%6<3)target-=3;
    //                            }
    //                            mbd.TemplateId=target;
                                mbd.TemplateId=(short)context.Random.Next(39,109);
                                var name=mbd.GetConfig().SubType.ToString();
                                if(name.Length>=3 && name[0]=='B'){
                                    mbd.TemplateId-=3;// Big* -> *
                                }
                            }
                        } else {
                            mbd.TemplateId=124;
                        }
                    }
                    MakeMaxResourceGrid(mbd);
                    mbd.CurrResources=mbd.MaxResources;
                    DomainManager.Map.SetBlockData(context,mbd);
                }
                {
                    var mbd = DomainManager.Map.GetBlock(areaId, blockId);
                    if(mbd.CanChangeBlockType() && ___IsDirect){
                        if(mbd.TemplateId>=109 && mbd.TemplateId<=124){
                            mbd.TemplateId=(short)context.Random.Next(39,109);
                            var name=mbd.GetConfig().SubType.ToString();
                            if(name.Length>=3 && name[0]=='B'){
                                mbd.TemplateId-=3;// Big* -> *
                            }
                        }
                        MakeMaxResourceGrid(mbd);
                        mbd.CurrResources=mbd.MaxResources;
                        DomainManager.Map.AddBlockMalice(context, areaId, blockId, 32767);
                    } else {
                        MakeMaxResourceGrid(mbd);
                        mbd.CurrResources=mbd.MaxResources;
                        DomainManager.Map.SetBlockData(context,mbd);
                    }
                }
            }
        }
        public static unsafe bool MakeMaxResourceGrid(GameData.Domains.Map.MapBlockData block){
            var configData=block.GetConfig();
            fixed(short*ptr=block.MaxResources.Items)for (sbyte resourceType = 0; resourceType < 6; resourceType ++) {
                short maxResource = configData.Resources[(int)resourceType];
                if (maxResource < 0) {
                    maxResource = (short)(Math.Abs(maxResource)* 5);
                }
                else if (maxResource != 0) {
                    maxResource = (short)(maxResource+25);
                }
                *(ptr + resourceType) = maxResource;
            }
            return block.CurrResources.GetSum()==block.MaxResources.GetSum();
        }
    }
}
